package edu.hubu.commons;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.annotations.Proxy;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author moonlan
 * @date 2020/10/20 下午2:23
 */
@ApiModel("结果Json串")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
@Proxy(lazy = false)
public class ResultsJson {
    @ApiModelProperty("程序状态码")
    private int code;
    @ApiModelProperty("程序信息")
    private String msg;
    @ApiModelProperty("data总共的长度")
    private long total;
    @ApiModelProperty("当前显示的data的页数")
    private long current;
    @ApiModelProperty("数据")
    private Object data;

    private ResultsJson() {
    }

    public ResultsJson(int code, String msg, long total, long current, Object data) {
        this.code = code;
        this.msg = msg;
        this.total = total;
        this.current = current;
        this.data = data;
    }

    public static ResultsJson toJson(StatusCode code, long total, long current, Object data) {
        return ResultsJson.builder().code(code.getCode()).msg(code.getMsg()).total(total).current(current).data(data).build();
    }

    public static ResultsJson toJson(Integer code, String msg, long total, long current, Object data) {
        return ResultsJson.builder().code(code).msg(msg).total(total).current(current).data(data).build();
    }

    public static void toJson(Integer code, String msg, long total, long current, Object data, HttpServletResponse response) {
        ObjectMapper mapper = new ObjectMapper();
        PrintWriter writer;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            String value = mapper.writeValueAsString(ResultsJson.builder().code(code).msg(msg).total(total).current(current).build());
            writer.write(value);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        } finally {
            writer.close();
        }
    }

    public static ResultsJsonBuilder builder() {
        return new ResultsJsonBuilder();
    }

    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public long getTotal() {
        return this.total;
    }

    public long getCurrent() {
        return this.current;
    }

    public Object getData() {
        return this.data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public void setCurrent(long current) {
        this.current = current;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean equals(final Object o) {
        if (o == this)
            return true;
        if (!(o instanceof ResultsJson))
            return false;
        final ResultsJson other = (ResultsJson) o;
        if (!other.canEqual((Object) this))
            return false;
        if (this.getCode() != other.getCode())
            return false;
        final Object this$msg = this.getMsg();
        final Object other$msg = other.getMsg();
        if (this$msg == null ? other$msg != null : !this$msg.equals(other$msg))
            return false;
        if (this.getTotal() != other.getTotal())
            return false;
        if (this.getCurrent() != other.getCurrent())
            return false;
        final Object this$data = this.getData();
        final Object other$data = other.getData();
        if (this$data == null ? other$data != null : !this$data.equals(other$data))
            return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof ResultsJson;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        result = result * PRIME + this.getCode();
        final Object $msg = this.getMsg();
        result = result * PRIME + ($msg == null ? 43 : $msg.hashCode());
        final long $total = this.getTotal();
        result = result * PRIME + (int) ($total >>> 32 ^ $total);
        final long $current = this.getCurrent();
        result = result * PRIME + (int) ($current >>> 32 ^ $current);
        final Object $data = this.getData();
        result = result * PRIME + ($data == null ? 43 : $data.hashCode());
        return result;
    }

    public String toString() {
        return "ResultsJson(code=" + this.getCode() + ", msg=" + this.getMsg() + ", total=" + this.getTotal() + ", current=" + this.getCurrent() + ", data=" + this.getData() + ")";
    }

    public static class ResultsJsonBuilder {
        private int code;
        private String msg;
        private long total;
        private long current;
        private Object data;

        ResultsJsonBuilder() {
        }

        public ResultsJsonBuilder code(int code) {
            this.code = code;
            return this;
        }

        public ResultsJsonBuilder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public ResultsJsonBuilder total(long total) {
            this.total = total;
            return this;
        }

        public ResultsJsonBuilder current(long current) {
            this.current = current;
            return this;
        }

        public ResultsJsonBuilder data(Object data) {
            this.data = data;
            return this;
        }

        public ResultsJson build() {
            return new ResultsJson(code, msg, total, current, data);
        }

        public String toString() {
            return "ResultsJson.ResultsJsonBuilder(code=" + this.code + ", msg=" + this.msg + ", total=" + this.total + ", current=" + this.current + ", data=" + this.data + ")";
        }
    }
}
